import ast
import pandas as pd
import os


def convert_ratings(input_fname, output_fname):
    rating = pd.DataFrame([], columns=['userID', 'itemID', 'rating'])
    with open(input_fname, 'r') as f:
        text = f.read()
        line_l = text.split("\n")
        for i, line in enumerate(line_l, 0):
            if line == '':
                continue
            if i % 1000 == 0:
                print("i {} len {}".format(i, len(line_l)))
            jins = ast.literal_eval(line)
            if 'username' in jins and 'product_id' in jins and 'hours' in jins:
                rating = rating.append(
                    {'userID': jins['username'], 'itemID': jins['product_id'], 'rating': jins['hours']},
                    ignore_index=True)

    order_l = ['userID', 'itemID', 'rating']
    rating = rating[order_l]
    rating.to_csv(output_fname, index=False)
    print("complete {}".format(output_fname))


if __name__ == '__main__':
    input_fname = os.path.join('/run/media/hdd/Recommendation/steam/test.json')
    output_fname = os.path.join('/run/media/hdd/Recommendation/steam/test_ratings.csv')
    convert_ratings(input_fname, output_fname)
